//
//  NewsViewController.m
//  BosidengIphone
//
//  Created by  on 11-12-29.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsCell.h"
#import "NewsData.h"
#import "ASIHTTPRequest.h"
#import "NewsImageViewController.h"
#import "MainViewController.h"

@implementation NewsViewController
@synthesize mTableView;
@synthesize newsArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.newsArray = [NewsData getAllItems];
    }
    return self;
}

- (void)dealloc
{
    [newsArray release];
    [mTableView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    mTableView.dataSource = self;
    mTableView.delegate = self;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [newsArray count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"NewsCellIdentifier";
    
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        //NSLog(@"alloc cell from xib path=%d", [indexPath row]);
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"NewsCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
    }
    // Configure the cell...
	NSUInteger row = [indexPath row];
	NewsData *rowData = [newsArray objectAtIndex:row];
    [cell config:self data:rowData indexPath:indexPath];
    return cell;
}

- (void)requestNewsImage:(NSString *)url localPath:(NSString *)path indexPath:(NSIndexPath *)indexPath {
    //NSLog(@"Request Image! URL=%@", url);
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
    [request setDownloadDestinationPath:path];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestComplete:)];
    [request setDidFailSelector:@selector(requestFailed:)];
    [request setUserInfo:[NSDictionary dictionaryWithObject:indexPath forKey:@"indexPath"]];
    [requestQueue addOperation:request];
}

- (void)requestComplete:(ASIHTTPRequest *)request {
    //NSLog(@"Image Request success! path=%@", [request downloadDestinationPath]);
    NSDictionary *uesrInfo = [request userInfo];
    NSIndexPath *indexPath = [uesrInfo objectForKey:@"indexPath"];
    NewsCell *cell = (NewsCell *)[self.mTableView cellForRowAtIndexPath:indexPath];
    if (cell) {
        //NSLog(@"reflash cell indexPath(%d, %d)", indexPath.row, [indexPath section]);
        int rowId = indexPath.row;
        NewsData *rowData = [newsArray objectAtIndex:rowId];
        [cell config:self data:rowData indexPath:indexPath];
        [cell setNeedsDisplay]; 
    }
}

- (void)jumpNewsImagesView:(NewsData *)data {
    NewsImageViewController *imagesView = [[NewsImageViewController alloc] initWithNibName:@"NewsImageViewController" bundle:nil];
    imagesView.imageUrls = [data.newsImgs componentsSeparatedByString: @"|"];
    MainViewController *mainView = (MainViewController *)mainViewController;
    [mainView pushViewController:imagesView];
    [imagesView release];
}

- (void)requestFailed:(ASIHTTPRequest *)request {
    NSLog(@"Image Request Failed! path=%@", [request downloadDestinationPath]);
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	//changeViewController();
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	NSUInteger row = [indexPath row];
	NewsData *rowData = [newsArray objectAtIndex:row];
    CGFloat height = [rowData getHeight];
    //NSLog(@"height=%.0f", height);
    return height;
}

@end
